home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Scene Storm
/
Scene Storm - Volume 1.iso
/
coding
/
c
/
hydra
/
fmisc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-12-20
|
7KB
|
222 lines
/*=============================================================================
HydraCom Version 1.00
=============================================================================*/
#include "hydracom.h"
#ifdef AMIGA
#define NAMELEN 32 /* OLSEN */
#else
#define NAMELEN 13
#endif
#define LINELEN 64
static char xfer_log[PATHLEN];
static boolean xfer_logged;
static char xfer_pathname[PATHLEN];
static char xfer_real[NAMELEN],
xfer_temp[NAMELEN];
static long xfer_fsize,
xfer_ftime;
void unique_name (char *pathname)
{
static char *suffix = ".000";
register char *p;
register int n;
if (fexist(pathname)) {
p = pathname;
while (*p && *p!='.') p++;
for (n=0; n<4; n++) {
if (!*p) {
*p = suffix[n];
*(++p) = '\0';
}
else
p++;
}
while (fexist(pathname)) {
p = pathname + ((int) strlen(pathname)) - 1;
if (!isdigit(*p))
*p = '0';
else {
for (n=3; n--;) {
if (!isdigit(*p)) *p = '0';
if (++(*p) <= '9') break;
else *p-- = '0';
}/*for*/
}
}/*while(exist)*/
}/*if(exist)*/
}/*unique_name()*/
char *xfer_init (char *fname, long fsize, long ftime)
{
char linebuf[LINELEN + 1];
char bad_real[NAMELEN],
bad_temp[NAMELEN];
long bad_fsize,
bad_ftime;
char *p;
FILE *fp;
if (single_done)
return (NULL);
strcpy(xfer_real,fname);
xfer_fsize = fsize;
xfer_ftime = ftime;
mergepath(xfer_pathname,download,xfer_real);
if (fexist(xfer_pathname)) {
struct stat f;
stat(xfer_pathname,&f);
if (xfer_fsize == f.st_size && xfer_ftime == f.st_mtime)
return (NULL); /* already have file */
}
mergepath(xfer_log,download,"BAD-XFER.LOG");
if ((fp = sfopen(xfer_log,"rt",DENY_WRITE)) != NULL) {
while (fgets(linebuf,LINELEN,fp)) {
sscanf(linebuf,"%s %s %ld %lo",
bad_real, bad_temp, &bad_fsize, &bad_ftime);
if (!strcmp(xfer_real,bad_real) &&
xfer_fsize == bad_fsize && xfer_ftime == bad_ftime) {
mergepath(xfer_pathname,download,bad_temp);
if (fexist(xfer_pathname)) {
fclose(fp);
strcpy(xfer_temp,bad_temp);
xfer_logged = true;
return (xfer_pathname);
}
}
}
fclose(fp);
}
strcpy(xfer_pathname,download);
p = xfer_pathname + ((int) strlen(xfer_pathname));
strcat(xfer_pathname,"BAD-XFER.000");
unique_name(xfer_pathname);
strcpy(xfer_temp,p);
xfer_logged = false;
return (xfer_pathname);
}/*xfer_init()*/
boolean xfer_bad (void)
{
struct stat f;
FILE *fp;
int n;
if (single_file[0])
single_done = true;
if (xfer_logged) /* Already a logged bad-xfer */
return (true);
n = ((int) strlen(xfer_real)) - 1;
if (n > 3 &&
(!strcmp(&xfer_real[n-3],".PKT") || !strcmp(&xfer_real[n-3],".REQ"))) {
xfer_del();
return (false); /* don't recover .PKT / .REQ */
}
stat(xfer_pathname,&f);
if (noresume || f.st_size < 1024L) { /* not allowed/worth saving */
xfer_del();
return (false);
}
if ((fp = sfopen(xfer_log,"at",DENY_WRITE)) != NULL) {
fprintf(fp,"%s %s %ld %lo\n",
xfer_real, xfer_temp, xfer_fsize, xfer_ftime);
fclose(fp);
return (true); /* bad-xfer logged now */
}
xfer_del();
return (false); /* Couldn't log bad-xfer */
}/*xfer_bad()*/
char *xfer_okay (void)
{
static char new_pathname[PATHLEN];
char *p;
strcpy(new_pathname,download);
p = new_pathname + ((int) strlen(new_pathname)); /* start of fname */
if (single_file[0]) {
strcat(new_pathname,single_file); /* add override fname */
single_done = true;
}
else {
strcat(new_pathname,xfer_real); /* add real fname */
unique_name(new_pathname); /* make it unique */
}
rename(xfer_pathname,new_pathname); /* rename temp to real */
if (!nostamp && xfer_ftime)
setstamp(new_pathname,xfer_ftime); /* set timestamp */
if (xfer_logged) /* delete from bad-xfer log */
xfer_del();
return (strcmp(p,xfer_real) ? p : NULL); /* dup rename? */
}
void xfer_del (void)
{
char new_log[PATHLEN];
char linebuf[LINELEN + 1];
char bad_real[NAMELEN],
bad_temp[NAMELEN];
long bad_fsize,
bad_ftime;
FILE *fp, *new_fp;
boolean left;
if (fexist(xfer_pathname))
unlink(xfer_pathname);
if ((fp = sfopen(xfer_log, "rt", DENY_WRITE)) != NULL) {
mergepath(new_log,download,"BAD-XFER.$$$");
if ((new_fp = sfopen(new_log, "wt", DENY_ALL)) != NULL) {
left = false;
while (fgets(linebuf,LINELEN,fp)) {
sscanf(linebuf,"%s %s %ld %lo",
bad_real, bad_temp, &bad_fsize, &bad_ftime);
if (strcmp(xfer_real,bad_real) ||
strcmp(xfer_temp,bad_temp) ||
xfer_fsize != bad_fsize || xfer_ftime != bad_ftime) {
fputs(linebuf,new_fp);
left = true;
}
}
fclose(fp);
fclose(new_fp);
unlink(xfer_log);
if (left) rename(new_log,xfer_log);
else unlink(new_log);
}
else
fclose(fp);
}
}/*xfer_del()*/
/* end of fmisc.c */